import os
from langchain.chat_models import init_chat_model
from langchain_core.prompts import ChatPromptTemplate

#  从环境变量中读取DeepSeek的API Key
key = os.getenv("DS_API_KEY")
# print(key)
api_key = str(key)

model = init_chat_model(
    model="deepseek-chat",
    base_url="https://api.deepseek.com",
    api_key=api_key
)

# 通常来说 需要把用户和提示词模板加入到用户输入里  templates
system_template = "Translate the following from English into {language}"
prompt_template = ChatPromptTemplate.from_messages(
    [("system", system_template), ("user", "{text}")]
)

# 格式化language 到 system_template中  格式化text 到 user中
prompt = prompt_template.invoke({"language": "Italian", "text": "hi!"})
# print(prompt)
# print(prompt.to_messages())

# response = model.invoke(prompt)
# print(response)
for token in model.stream(prompt):
    print(token.content, end="|")